﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using GMS.Dinasty.BO;

namespace GMS.BasicControls
{
    public partial class ConstructorStrips : BackGroundControl
    {
        public ConstructorStrips(DataGridView view)
        {
            InitializeComponent();
            _dataView = view;
        }
        DataGridView _dataView;
        DataTable source = new DataTable();
        void CreateSourceTable()
        {
            DataColumn dc = new DataColumn("scan");
            source.Columns.Add(dc);

            source.RowChanged += new DataRowChangeEventHandler(source_RowChanged);
            source.RowDeleted += new DataRowChangeEventHandler(source_RowDeleted);
        }

        void source_RowDeleted(object sender, DataRowChangeEventArgs e)
        {
            Calculate();
        }

        void source_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            Calculate();
        }
        private void ConstructorStrips_Load(object sender, EventArgs e)
        {
            CreateSourceTable();
            Good good = new Good(_dataView.CurrentCell.Value.ToString());
            _lenght = good.Lenght;
            textBox1.Text = good.Lenght.ToString();
            foreach(string s in good.Scans)
            {
                DataRow dr = source.NewRow();
                dr["scan"] = s;
                source.Rows.Add(dr);
            }
            dataGridView1.DataSource = source;
            this.BringToFront();
        }
        List<string> scans = new List<string>();
        public List<string> Scans
        {
            get { return scans; }
            set { scans = value; }
        }
        decimal square = 0;
        void Calculate()
        {
            int scann = 0;
            foreach (DataRow dr in source.Rows)
            {
                string s = dr["scan"].ToString();
                scann = scann + int.Parse(s);
                scans.Add(dr["scan"].ToString());
            }
            square = scann * decimal.Parse(textBox1.Text.ToString());
            label1.Text = square / 1000000 + " Кв.м.";
            label3.Text = scann.ToString();
        }
        private int _lenght;
        public int Lenght
        {
            get { return _lenght; }
            set { _lenght = value; }
        }
        private void applylinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            scans.Clear();
            Calculate();
            Good good = new Good(_dataView.CurrentCell.Value.ToString());
            good.Scans = scans;
            good.Lenght = int.Parse(textBox1.Text.ToString());
            _dataView.CurrentCell.Value = good.InputString();
            closeLabel_LinkClicked(sender, e);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Calculate();
        }
    }
}
